﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using App2Wpf.Models;
using App2Wpf.Views;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows.Input;
using System.Windows;
using HandyControl.Controls;
using HandyControl.Data;
using App2Wpf.FileServer;

namespace App2Wpf.ViewModels
{
    public class FirstVM : ObservableObject
    {
        //定义委托
        public delegate void ConnectClickedHandle(nameip m_nameip);

        public event ConnectClickedHandle ConnectEvent;

        public RenderingMode ChooseCardMode { get; set; }

        public FirstWindowModel _fModel;
        public FirstWindowModel fModel
        {
            get => _fModel;
            set => SetProperty(ref _fModel, value);
        }

        public nameip _Selected;
        public nameip Selected
        {
            get { return _Selected; }
            
            set {
              SetProperty(ref _Selected, value);
              comboBoxCurrent = _Selected.ip;
            }
        }
        private void mInit()
        {
            
            fModel = new FirstWindowModel();

        }

        public List<string> UiCombox { get; set; }

        public string _comboBoxCurrent;
        public string comboBoxCurrent { 
            get=> _comboBoxCurrent;
            set => SetProperty(ref _comboBoxCurrent, value);
        }

        private RelayCommand _btnconnect;
        public RelayCommand btnConnect
        {
            get { return _btnconnect; }
            set
            {
                SetProperty(ref _btnconnect, value);
            }
        }
        private void Connect()
        {
            if (comboBoxCurrent != null)
            {
                if(fModel.iplist.Where(p=> p.ip == comboBoxCurrent).ToArray().Length > 0)
                {
                    ConnectEvent(fModel.iplist.Where(p => p.ip == comboBoxCurrent).ToArray().First());
                    return;
                }
                ConnectEvent(new nameip { no = 0, name = comboBoxCurrent, ip = comboBoxCurrent });
                return;
            }
            string tipMsg = "IP为空，\r\n请先选择IP地址";
            Growl.Info(new GrowlInfo { Message = tipMsg, WaitTime = 2, ShowDateTime = true });
        }

        /// <summary>
        /// 调用了两次，源头？？？
        /// </summary>
        public FirstVM()
        {
            mInit();
            btnConnect = new RelayCommand(new Action(Connect));

        }

        private void mStartFTPserver()
        {
            Task.Run(() =>ftpServer.Start());
        }
    }
}
